home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / The Crash Manager / "source" / main.p < prev    next >
Encoding:
Text File  |  1998-06-20  |  8.0 KB  |  377 lines  |  [TEXT/CWIE]

  1. unit main;
  2.  
  3. interface
  4.  
  5. procedure DoMainLoop;
  6.  
  7. implementation
  8.  
  9. uses
  10.     globals,
  11.     Events,
  12.     Windows,
  13.     ToolUtils,
  14.     Devices,
  15.     AppleEvents,
  16.     Appearance,
  17.     Resources,
  18.     Dialogs,
  19.     cwindow,
  20.     thebigpicture;
  21.     
  22. function IsDeep(w:WindowPtr):boolean;
  23. begin
  24.     IsDeep:=true;
  25. end;
  26.  
  27. procedure Blit(p:PicHandle;r:Rect;w:WindowPtr);
  28. var
  29.     err:OSErr;
  30.     theGWorld:GWorldPtr;
  31.     lockFlag:boolean;
  32.     savePort:CGrafPtr;
  33.     saveDevice:GDHandle;
  34. begin
  35.     err:=NewGWorld(theGWorld,8,r,nil,nil,0);
  36.     lockFlag:=LockPixels(GetGWorldPixMap(theGWorld));
  37.     GetGWorld(savePort,saveDevice);
  38.     SetGWorld(theGWorld,nil);
  39.     DrawPicture(PicHandle(p),r);
  40.     SetGWorld(savePort,saveDevice);
  41.     CopyBits(GrafPtr(theGWorld)^.portBits,
  42.                         GrafPtr(w)^.portBits,
  43.                         r,
  44.                         r,
  45.                         srcCopy,
  46.                         nil);
  47.     DisposeGWorld(theGWorld);
  48. end;
  49.  
  50. procedure MaskBlit(p,m:PicHandle;r:Rect;w:WindowPtr);
  51. var
  52.     err:OSErr;
  53.     pictGWorld,maskGWorld:GWorldPtr;
  54.     savePort:CGrafPtr;
  55.     saveDevice:GDHandle;
  56. begin
  57.     EraseRect(r);
  58.     err:=NewGWorld(pictGWorld,32,r,nil,nil,0);
  59.     err:=NewGWorld(maskGWorld,32,r,nil,nil,0);
  60.     GetGWorld(savePort,saveDevice);
  61.     SetGWorld(pictGWorld,nil);
  62.     DrawPicture(PicHandle(p),r);
  63.     SetGWorld(maskGWorld,nil);
  64.     DrawPicture(PicHandle(m),r);
  65.     SetGWorld(savePort,saveDevice);
  66.     ForeColor(blackColor);
  67.     BackColor(whiteColor);
  68.     CopyDeepMask(GrafPtr(pictGWorld)^.portBits,
  69.                     GrafPtr(maskGWorld)^.portBits,
  70.                     GrafPtr(w)^.portBits,
  71.                     r,
  72.                     r,
  73.                     r,
  74.                     srcCopy,
  75.                     nil);
  76.     DisposeGWorld(maskGWorld);
  77.     DisposeGWorld(pictGWorld);
  78. end;
  79.     
  80. procedure DoPICTProc(theWindow:WindowPtr;itemNo:integer);
  81. var
  82.     pict,mask:Handle;
  83.     theRect:Rect;
  84.     theType:integer;
  85.     theControl:Handle;
  86. begin
  87.     GetDialogItem(DialogPtr(theWindow),itemNo,theType,theControl,theRect);
  88.     if IsDeep(theWindow) then begin
  89.         case itemNo of
  90.             ABOUT_TITLE_UI:begin
  91.                 pict:=GetResource('PICT',ABOUT_TITLE_DEEP_ID);
  92.                 Blit(PicHandle(pict),theRect,theWindow);
  93.                 ReleaseResource(pict);
  94.             end;
  95.             COPYR_UI:begin
  96.                 pict:=GetResource('PICT',COPYR_DEEP_ID);
  97.                 Blit(PicHandle(pict),theRect,theWindow);
  98.                 ReleaseResource(pict);
  99.             end;
  100.             ABOUT_ICON_UI:begin
  101.                 pict:=GetResource('PICT',ABOUT_ICON_DEEP_ID);
  102.                 mask:=GetResource('PICT',ABOUT_ICON_MASK_ID);
  103.                 MaskBlit(PicHandle(pict),PicHandle(mask),theRect,theWindow);
  104.                 ReleaseResource(mask);
  105.                 ReleaseResource(pict);
  106.             end;
  107.         end;
  108.     end else begin
  109.         case itemNo of
  110.             ABOUT_TITLE_UI:begin
  111.                 pict:=GetResource('PICT',ABOUT_TITLE_BW_ID);
  112.                 DrawPicture(PicHandle(pict),theRect);
  113.                 ReleaseResource(pict);
  114.             end;
  115.             COPYR_UI:begin
  116.                 pict:=GetResource('PICT',COPYR_BW_ID);
  117.                 DrawPicture(PicHandle(pict),theRect);
  118.                 ReleaseResource(pict);
  119.             end;
  120.             ABOUT_ICON_UI:begin
  121.                 pict:=GetResource('PICT',ABOUT_ICON_BW_ID);
  122.                 DrawPicture(PicHandle(pict),theRect);
  123.                 ReleaseResource(pict);
  124.             end;
  125.         end;
  126.     end;
  127. end;
  128.     
  129. procedure DoAboutBox;
  130. var
  131.     aboutBox:DialogPtr;
  132.     thePICTProc:UserItemUPP;
  133.     theType,theItem:integer;
  134.     junkHandle:Handle;
  135.     theBox:Rect;
  136.     doneDialog:boolean;
  137.     theErr:OSErr;
  138. begin
  139.     aboutBox:=GetNewDialog(ABOUT_BOX_ID,nil,WindowPtr(-1));
  140.     thePICTProc:=NewUserItemProc(@DoPICTProc);
  141.     GetDialogItem(aboutBox,ABOUT_ICON_UI,theType,junkHandle,theBox);
  142.     SetDialogItem(aboutBox,ABOUT_ICON_UI,theType,Handle(thePICTProc),theBox);
  143.     GetDialogItem(aboutBox,COPYR_UI,theType,junkHandle,theBox);
  144.     SetDialogItem(aboutBox,COPYR_UI,theType,Handle(thePICTProc),theBox);
  145.     GetDialogItem(aboutBox,ABOUT_TITLE_UI,theType,junkHandle,theBox);
  146.     SetDialogItem(aboutBox,ABOUT_TITLE_UI,theType,Handle(thePICTProc),theBox);
  147.     theErr:=SetDialogDefaultItem(aboutBox,OK_BUTTON);
  148.     ShowWindow(aboutBox);
  149.     doneDialog:=false;
  150.     while not doneDialog do begin
  151.         ModalDialog(nil,theItem);
  152.         case theItem of
  153.             OK_BUTTON: begin
  154.                 doneDialog:=true;
  155.             end;
  156.         end;
  157.     end;
  158.     DisposeRoutineDescriptor(thePICTProc);
  159.     DisposeDialog(aboutBox);
  160. end;
  161.     
  162. procedure DoAppleMenu(choice:integer);
  163. var
  164.     accName: Str255;
  165.     accNumber:integer;
  166. begin
  167.     case choice of
  168.         APPLE_ABOUT:
  169.             DoAboutBox;
  170.         otherwise begin
  171.             GetMenuItemText(gAppleMenuH, choice, accName);
  172.             accNumber := OpenDeskAcc(accName);
  173.         end;
  174.     end;
  175. end;
  176.  
  177. procedure DoFileMenu(choice:integer);
  178. begin
  179.     case choice of
  180.         FILE_QUIT,FILE_CLOSE:
  181.             gQuitNow:=true;
  182.     end;
  183.         
  184. end;
  185.  
  186. procedure DoEditMenu(choice:integer);
  187. begin
  188.     case choice of
  189.         EDIT_CUT:
  190.             ;
  191.         EDIT_COPY:
  192.             ;
  193.         EDIT_PASTE:
  194.             ;
  195.         EDIT_CLEAR:
  196.             ;
  197.         EDIT_UNDO:
  198.             ;
  199.     end;    
  200. end;
  201.     
  202. procedure HandleMenuChoice (menuChoice: longint);
  203. var
  204.     theMenu, theMenuItem: integer;
  205. begin
  206.     if menuChoice <> 0 then begin
  207.         theMenu := HiWord(menuChoice);
  208.         theMenuItem := LoWord(menuChoice);
  209.         case theMenu of
  210.             APPLE_MENU:
  211.                 DoAppleMenu(theMenuItem);
  212.             FILE_MENU:
  213.                 DoFileMenu(theMenuItem);
  214.             EDIT_MENU:
  215.                 DoEditMenu(theMenuItem);
  216.         end;
  217.     end;
  218. end;
  219.  
  220. procedure DoContentClick(p:Point;w:WindowPtr);
  221. var
  222.     thePart:integer;
  223.     theControl:ControlHandle;
  224. begin
  225.     GlobalToLocal(p);
  226.     thePart:=FindControl(p,w,theControl);
  227.     if thePart <> 0 then begin
  228.         thePart:=TrackControl(theControl,p,nil);
  229.         if thePart <>0 then begin
  230.             if theControl=gBombButton then begin
  231.                 gPaneState:=PANE_BOMB;
  232.                 SetControlValue(gClusterButton,0);
  233.                 DrawMainPane(kThemeStateActive);
  234.             end;
  235.             if theControl=gClusterButton then begin
  236.                 gPaneState:=PANE_CLUSTER;
  237.                 SetControlValue(gBombButton,0);
  238.                 DrawMainPane(kThemeStateActive);
  239.             end;
  240.             if theControl=gTypePopUp then begin
  241.                 DrawSmallPic;
  242.             end;
  243.             if theControl=gPreviewButton then begin
  244.                 DrawBigPicture;
  245.             end;
  246.         end;
  247.     end;
  248. end;
  249.     
  250. procedure HandleMouseDown;
  251. var    
  252.     thePart: integer;
  253.     whichWindow: WindowPtr;
  254.     menuChoice: longint;
  255. begin
  256.     thePart := FindWindow(gEr.where, whichWindow);
  257.     case thePart of
  258.         inMenuBar:  begin
  259.             menuChoice := MenuSelect(gEr.where);
  260.             HandleMenuChoice(menuChoice);
  261.             HiliteMenu(0);
  262.             DrawMenuBar;
  263.         end;
  264.         inSysWindow:
  265.             SystemClick(gEr, whichWindow);
  266.         inDrag:
  267.             DragWindow(whichWindow, gEr.where, qd.screenBits.bounds);
  268.         inContent:
  269.             DoContentClick(gEr.where,whichWindow);
  270.         inGoAway:if TrackGoAway(whichWindow,gEr.where) then 
  271.             gQuitNow:=true;
  272.     end;
  273. end;
  274.  
  275. procedure DoHighLevelEvent (event: EventRecord);
  276. {This is pretty lame here since I'm only implementing the core events.}
  277. var
  278.     myErr: OSErr;
  279. begin
  280.     myErr := AEProcessAppleEvent(event);
  281. end;
  282.  
  283. function IsDAWindow (whichWindow: WindowPtr): boolean;
  284. {Stupid routine to determine if a window belongs to us or a desk accessory.    }
  285. begin
  286.     if whichWindow = nil then
  287.         IsDAWindow := false
  288.     else
  289.         IsDAWindow := (WindowPeek(whichWindow)^.windowKind < 0);
  290. end;
  291.  
  292. procedure DeactivateControls;
  293. var
  294.     err:OSErr;
  295. begin
  296.     err:=DeactivateControl(gBombButton);
  297.     err:=DeactivateControl(gClusterButton);
  298.     err:=DeactivateControl(gTypePopUp);
  299.     err:=DeactivateControl(gPreviewButton);
  300.     err:=DeactivateControl(gFreqPopUp);
  301. end;
  302.  
  303. procedure ActivateControls;
  304. var
  305.     err:OSErr;
  306. begin
  307.     err:=ActivateControl(gBombButton);
  308.     err:=ActivateControl(gClusterButton);
  309.     err:=ActivateControl(gTypePopUp);
  310.     err:=ActivateControl(gPreviewButton);
  311.     err:=ActivateControl(gFreqPopUp);
  312. end;
  313.  
  314. procedure WNE;
  315. var
  316.     importantEvent:boolean;
  317.     oldPort:GrafPtr;
  318. begin
  319.     importantEvent:=WaitNextEvent(everyEvent,gEr,EVENT_DELAY,nil);
  320.     if importantEvent then begin
  321.         case gEr.what of
  322.             mouseDown:
  323.                 HandleMouseDown;
  324.             mouseUp:
  325.                 ;
  326.             kHighLevelEvent: 
  327.                 DoHighLevelEvent(gEr);
  328.             updateEvt:begin
  329.                 if not IsDAWindow(WindowPtr(gEr.message)) then begin
  330.                     GetPort(oldPort);
  331.                     SetPort(WindowPtr(gEr.message));
  332.                     BeginUpdate(WindowPtr(gEr.message));
  333.                     if WindowPtr(gEr.message)=gMainWindow then begin
  334.                         if gInForeground=true then
  335.                             DrawWindow(kThemeStateActive)
  336.                         else
  337.                             DrawWindow(kThemeStateDisabled);
  338.                     end;
  339.                     EndUpdate(WindowPtr(gEr.message));
  340.                     SetPort(oldPort);
  341.                 end;
  342.             end;
  343.             osEvt:begin
  344.                 if BTST(gEr.message,0) then begin
  345.                     SetCursor(qd.arrow);
  346.                     gInForeground:=true;
  347.                     DrawWindow(kThemeStateActive);
  348.                     ActivateControls;
  349.                 end else begin
  350.                     gInForeground:=false;
  351.                     DrawWindow(kThemeStateDisabled);
  352.                     DeactivateControls;
  353.                 end;
  354.                 DrawControls(gMainWindow);
  355.             end;
  356.             activateEvt,diskEvt:
  357.                 ;
  358.             keyDown:begin
  359.                 HandleMenuChoice(MenuEvent(gEr));
  360.                 HiliteMenu(0);
  361.             end;
  362.             keyUp:
  363.                 ;
  364.         end;
  365.     end;
  366. end;
  367.  
  368.  
  369. procedure DoMainLoop;
  370. begin
  371.     while not gQuitNow do begin
  372.         WNE;
  373.     end;
  374. end;
  375.  
  376.  
  377. end.